/****************************************************************************
 *  QLyrics - A simple lyrics viewer.                                       *
 *  Copyright (C) 2008  hsys <hanshuiys@gmail.com>                          *
 *                                                                          *
 *  This program is free software; you can redistribute it and/or modify    *
 *  it under the terms of the GNU General Public License as published by    *
 *  the Free Software Foundation; either version 3 of the License, or       *
 *  (at your option) any later version.                                     *
 *                                                                          *
 *  This program is distributed in the hope that it will be useful,         *
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of          *
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           *
 *  GNU General Public License for more details.                            *
 *                                                                          *
 *  You should have received a copy of the GNU General Public License       *
 *  along with this program; if not, write to the Free Software Foundation, *
 *  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA       *
 ****************************************************************************/

#include "lyricslayeredpanel.h"
#include "configmanager.h"
#include <QPixmap>

#ifndef Q_WS_X11
#include <windows.h>
#pragma comment(lib, "user32.lib")
#pragma comment(lib, "gdi32.lib")
#endif

LyricsLayeredPanel::LyricsLayeredPanel(QWidget* parent) : LyricsPanel(parent) {
    m_window = NULL;
    m_background = QImage(1, 1, QImage::Format_ARGB32_Premultiplied);
};

void LyricsLayeredPanel::paintEvent(QPaintEvent*) {
#ifdef Q_WS_X11
    QPainter p(this);
    p.setRenderHint(QPainter::Antialiasing);
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.fillRect(rect(), Qt::transparent);
    p.drawPixmap(0, 0, m_pixmap);
#endif
};

void LyricsLayeredPanel::redraw() {
    ConfigManager* cfg = ConfigManager::getInstance();
    //update();
    //m_painter->draw(this, m_startTime.elapsed());
    m_pixmap = QPixmap::fromImage(m_background);

    m_painter->setRect(cfg->value("Style/CropRect", rect()).toRect());
    m_painter->draw(&m_pixmap, m_startTime.elapsed());

#ifdef Q_WS_WIN32
    SetWindowLong(m_window->winId(), GWL_EXSTYLE,
        GetWindowLong(m_window->winId(), GWL_EXSTYLE) | WS_EX_LAYERED);
    HBITMAP bmp = m_pixmap.toWinHBITMAP(QPixmap::PremultipliedAlpha);
    HDC hDC = CreateCompatibleDC(NULL);
    SelectObject(hDC, bmp);

    SIZE sz = {m_pixmap.width(), m_pixmap.height()};
    POINT ptSource = {0};
    BLENDFUNCTION bf = { AC_SRC_OVER, 0, 255, AC_SRC_ALPHA };

    UpdateLayeredWindow(m_window->winId(), NULL, NULL, &sz, hDC,
        &ptSource, 0, &bf, ULW_ALPHA);

    DeleteObject(bmp);
    DeleteDC(hDC);
#else
    update();
#endif
};

void LyricsLayeredPanel::showEvent(QShowEvent*) {
    ConfigManager* cfg = ConfigManager::getInstance();
    // find out the window widget
    m_window = this;
    while (m_window->parentWidget() != 0)
        m_window = m_window->parentWidget();

    if (cfg->contains("Window/BackgroundImage")) {
        m_background.load(cfg->value("Window/BackgroundImage", QString()).toString());
        m_window->resize(m_background.size());
    } else {
        m_background.fill(cfg->value("Style/BgColor", Qt::black).value<QColor>().rgb());
        m_background = m_background.scaled(m_window->size());
    };
    m_background.convertToFormat(QImage::Format_ARGB32_Premultiplied);

    //SetWindowLong(m_window->winId(), GWL_EXSTYLE,
    //    GetWindowLong(m_window->winId(), GWL_EXSTYLE) | WS_EX_LAYERED);

    redraw();
};

